/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.adapter.DetailAdapter;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;
import com.mingmen.canting.model.PeiSongDetailModel;
import com.mingmen.canting.model.ShopDetailModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class ShopDetailActivity extends Activity {

    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.linear_return)
    LinearLayout linearReturn;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;


    String token;
    String supplierId;
    @BindView(R.id.billId)
    TextView billId;
    String shipping;

    @BindView(R.id.shipping_peiSong)
    TextView shipping_peiSong;

    @BindView(R.id.text_peisongfei)
    TextView text_peisongfei;
    @BindView(R.id.confirmPrice)
    TextView confirmPrice;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.receiveAddress)
    TextView receiveAddress;
    @BindView(R.id.receiveLinkman)
    TextView receiveLinkman;
    @BindView(R.id.receiveLinkTel)
    TextView receiveLinkTel;
    @BindView(R.id.supplierName)
    TextView supplierName;
    @BindView(R.id.layout_cancelOrder)
    LinearLayout layout_cancelOrder;
    @BindView(R.id.recycle_list)
    RecyclerView recycle_list;
    LinearLayoutManager linearLayoutManager;

    DetailAdapter detailAdapter;
    List<ShopDetailModel.DataBean.DetailsBean> dataBeanList = new ArrayList<>();
    String partCancelFlag;
    String partArriveFlag;
    String partRefuseFlag;

    String getsupplierName;
    String getShipping;
    @BindView(R.id.layout_shop)
    LinearLayout layout_shop;
    @BindView(R.id.layout_peisong)
    LinearLayout layout_peisong;
    @BindView(R.id.imglogo)
    ImageView imglogo;
    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopdetail);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        ImmersionBar.with(this).titleBar(mToolbar)
                .navigationBarColor(R.color.black).init();
        mContext = this;
        token = (String) SharePreferenceUtil.get(this, "token", "");
        supplierId = getIntent().getStringExtra("supplierId");
        partCancelFlag = getIntent().getStringExtra("partCancelFlag");
        partArriveFlag = getIntent().getStringExtra("partArriveFlag");
        partRefuseFlag = getIntent().getStringExtra("partRefuseFlag");
        getsupplierName = getIntent().getStringExtra("supplierName");
        getShipping = getIntent().getStringExtra("shipping");



        linearLayoutManager = new LinearLayoutManager(this);
        recycle_list.setLayoutManager(linearLayoutManager);
        detailAdapter = new DetailAdapter(this, dataBeanList);
        Log.d("supplierId", supplierId);
        recycle_list.setAdapter(detailAdapter);
        HttpResponse.getOrderInfo(token, supplierId, partCancelFlag, partArriveFlag, partRefuseFlag, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Log.d("ShopDetailActivityShopDetailActivity", s);
                ShopDetailModel peiSongDetailModel = new ShopDetailModel();
                peiSongDetailModel = GsonUtil.GsonToBean(s, ShopDetailModel.class);
                billId.setText(peiSongDetailModel.getData().get(0).getBillId());
                shipping = peiSongDetailModel.getData().get(0).getShipping();
                if (shipping.equals("0")) {

                    shipping_peiSong.setText("自取");
                    text_peisongfei.setText("配送费(" + "自取" + ")");
//                    1-商家配送
                } else if (shipping.equals("1")) {

                    shipping_peiSong.setText("商家配送");
                    text_peisongfei.setText("配送费(" + "商家配送" + ")");
                } else {
                    shipping_peiSong.setText("自取");
                    text_peisongfei.setText("配送费(" + "自取" + ")");
                }
//                commodityName.setText(peiSongDetailModel.getData().get(0).getDetails().get(0).getCommodityName());
//                price.setText(peiSongDetailModel.getData().get(0).getDetails().get(0).getPrice() + "");
                confirmPrice.setText("￥ "+peiSongDetailModel.getData().get(0).getDetails().get(0).getConfirmPrice() + "");
                total.setText("小计 ￥" + peiSongDetailModel.getData().get(0).getTotal() + "");
                receiveAddress.setText(peiSongDetailModel.getData().get(0).getReceiveAddress());
                receiveLinkman.setText(peiSongDetailModel.getData().get(0).getReceiveLinkman());
                receiveLinkTel.setText(peiSongDetailModel.getData().get(0).getReceiveLinkTel());
                supplierName.setText(peiSongDetailModel.getData().get(0).getSupplierName());
                dataBeanList.addAll(peiSongDetailModel.getData().get(0).getDetails());
                detailAdapter.setDataList(dataBeanList);
                Glide.with(mContext).load(AppConfig.IMGURL + peiSongDetailModel.getData().get(0).getLogo()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(imglogo);
                getShipping=peiSongDetailModel.getData().get(0).getShipping();
                if (getsupplierName.equals("线下自采")) {
                    layout_shop.setVisibility(View.GONE);
                    layout_peisong.setVisibility(View.GONE);
                    textCenter.setText("线下自采订单详情");
                }else {
                    if (getShipping.equals("0")) {
                        textCenter.setText("自取订单详情");
                        layout_peisong.setVisibility(View.GONE);
                    } else if (getShipping.equals("1")) {

                        textCenter.setText("商家配送订单详情");
                        layout_shop.setVisibility(View.VISIBLE);
                        layout_peisong.setVisibility(View.VISIBLE);
                    }
                }

            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick({R.id.text_return, R.id.linear_return, R.id.layout_cancelOrder})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
                finish();
                break;
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_register:
                break;
            case R.id.layout_cancelOrder:
                HttpResponse.cancelOrderAll(token, supplierId, new StringCallback(this) {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        BaseBean baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                        if (baseBean.getCode() == 0) {
                            EventBus.getDefault().post(2);
                            finish();

                        }

                        Toast.makeText(ShopDetailActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                    }
                });
                break;
        }
    }

    public void onEventMainThread(Integer a) {


    }
}